package com.fansaikj.erp.handler;

import org.springframework.stereotype.Component;
import xyz.erupt.annotation.fun.OperationHandler;
import xyz.erupt.core.constant.MenuStatus;
import xyz.erupt.core.constant.MenuTypeEnum;
import xyz.erupt.generator.model.GeneratorClass;
import xyz.erupt.jpa.dao.EruptDao;
import xyz.erupt.upms.model.EruptMenu;

import javax.annotation.Resource;
import java.util.List;

@Component
public class ImportGenTableHandlerImpl implements OperationHandler<GeneratorClass, Void> {

    @Resource
    private EruptDao eruptDao;

    @Override
    public String exec(List<GeneratorClass> data, Void unused, String[] param) {
        // 查询所有表
        List<GeneratorClass> classList = eruptDao.getJdbcTemplate().queryForList("select table_name , table_comment as name, create_time, update_time " +
                "from information_schema.tables where table_schema = (select database()) and table_name like 'erp_'", GeneratorClass.class);
        for (GeneratorClass generatorClass : classList) {
            String tableName = generatorClass.getTableName();
            String[] nameSplit = tableName.split("_");
            StringBuffer sb = new StringBuffer();
            for (String str : nameSplit) {
                sb.append(str.substring(0, 1).toUpperCase() + str.substring(1));
            }
            generatorClass.setClassName(sb.toString());
            eruptDao.persist(generatorClass);
            eruptDao.flush();
            // 列表菜单
            EruptMenu menu = new EruptMenu();
            menu.setCode(generatorClass.getClassName());
            menu.setName(generatorClass.getName());
            menu.setStatus(MenuStatus.OPEN.getValue());
            menu.setType(MenuTypeEnum.TABLE.getCode());
            menu.setValue(generatorClass.getClassName());
            menu.setSort(100);
            eruptDao.persist(menu);
            eruptDao.flush();
            // 新增
            EruptMenu addMenu = new EruptMenu();
            addMenu.setCode(generatorClass.getClassName() + "@ADD");
            addMenu.setName("新增");
            addMenu.setStatus(MenuStatus.OPEN.getValue());
            addMenu.setType(MenuTypeEnum.BUTTON.getCode());
            addMenu.setValue(generatorClass.getClassName() + "@ADD");
            addMenu.setSort(10);
            addMenu.setParentMenu(menu);
            eruptDao.persist(addMenu);
            eruptDao.flush();
            // 修改
            EruptMenu updateMenu = new EruptMenu();
            updateMenu.setCode(generatorClass.getClassName() + "@EDIT");
            updateMenu.setName("修改");
            updateMenu.setStatus(MenuStatus.OPEN.getValue());
            updateMenu.setType(MenuTypeEnum.BUTTON.getCode());
            updateMenu.setValue(generatorClass.getClassName() + "@EDIT");
            updateMenu.setSort(20);
            updateMenu.setParentMenu(menu);
            eruptDao.persist(updateMenu);
            eruptDao.flush();
            // 删除
            EruptMenu delMenu = new EruptMenu();
            delMenu.setCode(generatorClass.getClassName() + "@DELETE");
            delMenu.setName("删除");
            delMenu.setStatus(MenuStatus.OPEN.getValue());
            delMenu.setType(MenuTypeEnum.BUTTON.getCode());
            delMenu.setValue(generatorClass.getClassName() + "@DELETE");
            delMenu.setSort(30);
            delMenu.setParentMenu(menu);
            eruptDao.persist(delMenu);
            eruptDao.flush();
            // 导出
            EruptMenu expMenu = new EruptMenu();
            expMenu.setCode(generatorClass.getClassName() + "@EXPORT");
            expMenu.setName("导出");
            expMenu.setStatus(MenuStatus.OPEN.getValue());
            expMenu.setType(MenuTypeEnum.BUTTON.getCode());
            expMenu.setValue(generatorClass.getClassName() + "@EXPORT");
            expMenu.setSort(40);
            expMenu.setParentMenu(menu);
            eruptDao.persist(expMenu);
            eruptDao.flush();
            // 导入
            EruptMenu impMenu = new EruptMenu();
            impMenu.setCode(generatorClass.getClassName() + "@IMPORTABLE");
            impMenu.setName("导入");
            impMenu.setStatus(MenuStatus.OPEN.getValue());
            impMenu.setType(MenuTypeEnum.BUTTON.getCode());
            impMenu.setValue(generatorClass.getClassName() + "@IMPORTABLE");
            impMenu.setSort(50);
            impMenu.setParentMenu(menu);
            eruptDao.persist(impMenu);
            eruptDao.flush();
            // 详情
            EruptMenu infoMenu = new EruptMenu();
            infoMenu.setCode(generatorClass.getClassName() + "@VIEW_DETAIL");
            infoMenu.setName("详情");
            infoMenu.setStatus(MenuStatus.OPEN.getValue());
            infoMenu.setType(MenuTypeEnum.BUTTON.getCode());
            infoMenu.setValue(generatorClass.getClassName() + "@VIEW_DETAIL");
            infoMenu.setSort(60);
            infoMenu.setParentMenu(menu);
            eruptDao.persist(infoMenu);
            eruptDao.flush();
        }
        return "";
    }
}
